$(function () {
    if (typeof scope == 'undefined') {
        scope = {};
    }
    var navMoreConfig = {oNavBox: $('#j_navShowBox'), oNavBtn: $('#j_navShowBtn')}

    function navMore(options) {
        var oNavBox = options.oNavBox, oNavBtn = options.oNavBtn;
        oNavBtn.on('click tap', function () {
            var flag = oNavBtn[0].className;
            if (flag == 'down') {
                oNavBox.removeClass('hide').addClass('show'), oNavBtn.html('收起').addClass('up').removeClass('down');
                oNavBtn.data('close', 'up');
            } else {
                oNavBox.removeClass('show').addClass('hide'), oNavBtn.html('更多').addClass('down').removeClass('up');
                oNavBtn.data('close', 'down');
            }
        });
    }

    var scrollToTopConfig = {oTop: $('#back_to_top'), oComment_nav1: $('#j_comment_nav1'), oComment_nav2: $('#j_comment_nav2'), oBackTop: $('.j_backTop ')}

    function scrollToTop(options) {
        var toggleTimer = null, oTop = options.oTop;
        oComment_nav1 = options.oComment_nav1, oComment_nav2 = options.oComment_nav2, oBackTop = options.oBackTop;
        if (oTop.length > 0) {
            var sTop = 0, rTop = 0, pageHeight = document.documentElement.clientHeight;
            addOnscroll(function () {
                sTop = document.body.scrollTop || document.documentElement.scrollTop;
                var winHeight = $('body').height();
                var pHeight = winHeight - sTop;
                if (sTop < winHeight / 2) {
                    oTop.hide();
                } else {
                    oTop.show();
                }
                rTop = sTop;
            });
            oTop.click(function () {
                oComment_nav1.removeClass('fixednav');
                oComment_nav2.addClass('hide');
                document.body.scrollTop = document.documentElement.scrollTop = 0;
            });
            oBackTop.bind('click', function () {
                oComment_nav1.removeClass('fixednav');
                oComment_nav2.addClass('hide');
            });
        }
    }


    function addOnscroll(fn) {
        if (typeof window.onscroll == 'function') {
            var tempFn = window.onscroll;
            window.onscroll = function () {
                tempFn();
                fn();
            }
        } else {
            window.onscroll = function () {
                fn();
            }
        }
    }


    var channelConfig = {};
    channelConfig.loadMoreApplyURL = scope.load_api, channelConfig.page = 1, channelConfig.load_num = scope.load_num,  channelConfig.show_num = scope.page_size;
    var columnConfig = {};
    columnConfig.loadMoreApplyURL = scope.load_api, columnConfig.page = 1, columnConfig.show_num = scope.show_num, columnConfig.load_num = scope.load_num, columnConfig.iscolumn = scope.iscolumn || '';
    var pullLoaderConfig = {};
    pullLoaderConfig.loading = 0;
    pullLoaderConfig.isload = 0;
    pullLoaderConfig.oPullLoader = $('#j_pullLoader');

    function pullLoader() {
        window.addEventListener('scroll', addcomment, false);
    }

    function addcomment(ev) {
        if (pullLoaderConfig.loading == 0) {
            var sTop = document.body.scrollTop || document.documentElement.scrollTop, dHeight = $(document).height(), cHeight = document.documentElement.clientHeight;
            if (sTop + cHeight >= dHeight - cHeight) {
                pullLoaderConfig.loading = 1;
                pullLoaderConfig.oPullLoader.removeClass('hide');
                if (!pullLoaderConfig.isload) {
                    setTimeout(function () {
                        loadMoreApply();
                    }, 100)
                    pullLoaderConfig.isload = 1;
                }
                else {
                    loadMoreApply();
                }
                if (window.suda) {
                    var sudaName = pullLoaderConfig.oPullLoader.data('sudaclick');
                    if (sudaName) {
                        var clickInfo = {'type': 'feedloadMore', 'name': sudaName, 'title': 'feed流上拉加载统计', 'index': 0}
                    }
                    if (window.suds_count) {
                        window.suds_count(clickInfo);
                    }
                }
                ;
            }
        }
    }

    function loadMoreApply() {
        var HXid = $('#j_items_list').data('hxid');
        var FGid = $('#j_items_list').data('fgid');
        if (columnConfig.iscolumn) {
            var url = columnConfig.loadMoreApplyURL, page = columnConfig.page, show_num = columnConfig.show_num;

              var data = {HXid: HXid, FGid: FGid, show_num: columnConfig.show_num, page: columnConfig.page, act: 'more', jsoncallback: 'callbackFunction'}
            //var data = {minpr:minpr,maxpr:maxpr,wylx:wylx,city:city, show_num: show_num,  pageSize: columnConfig.show_num, page: columnConfig.page, act: 'more', jsoncallback: 'callbackFunction'}
        }
        else {
            var url = channelConfig.loadMoreApplyURL, page = channelConfig.page, show_num = channelConfig.show_num;
            var data = {HXid: HXid, FGid: FGid, show_num: columnConfig.show_num, page: columnConfig.page, act: 'more', jsoncallback: 'callbackFunction'}
        }
        if (!!url == false) {
            return false;
        }
        if (url.indexOf('?') == -1) {
            url = url + '?';
        }
        ajax(url, data, success, error, complate);
        function error() {
        }

        function success() {
        }

        function complate() {
        }
    }

    function ajax(url, data, success, error, complate) {
        $.ajax({url: url, data: data, async: false, type: 'GET', dataType: 'jsonp', success: function (data) {
            success && success();
            complate && complate();
        }, error: function (xhr, type) {
            error && error();
            complate && complate();
        }});
    }

    window.callbackFunction = function (rs) {
        if (columnConfig.iscolumn) {
            var total = DataToal;
            if (rs.status == 1 || rs.status == '1') {
                if ($('.star_nav').length > 0) {
                    var type = $('.star_nav').data('type');
                    var oWrap = $('#j_' + type + '_list');
                }
                else {
                    var oWrap = $('#j_items_list').find('.carditems').last();
                }
                var data = rs.data;
                var len = data.length, arr = [];
                var repeat;
                var dataCid = [];
                var aA = oWrap.find('a');
                aA.each(function () {
                    dataCid.push($(this).data('cid'));
                });
                for (var i = 0; i < len; i++) {
                    repeat = false;
                    for (var j = 0; j < dataCid.length; j++) {
                        if (data[i]._id == dataCid[j]) {
                            repeat = true;
                        }
                    }
                    if (repeat) {
                        continue;
                    }
                        arr.push('<a href = "javascript:void(0);"  data-images="'+data[i].Images+'" data-title="'+data[i].YbjName+'" data-cid="'+data[i].ID+'" class="aImg" >');
                        arr.push('<dl class="carditems_list" > ');

                        dt_push_column(data);
                        dd_push_column(data);
                        arr.push('</dl></a>');



                        function dt_push_column() {
                            if ($('#j_activeRss').hasClass('ar_wrap')) {
                                arr.push('<dt class="carditems_list_dt hide">');
                            }
                            else {
                                arr.push('<dt class="carditems_list_dt">');
                            }

                            arr.push('<img src="' + Home_Art_URL_IMAGE + "/home/" +data[i].Miniature  + '" alt="" onerror="nofind();" >');
                            arr.push('</dt>');
                        }

                        function dd_push_column() {
                            arr.push('<dd class="carditems_list_dd">');

                            arr.push('<div class="carditems_list_op">');
                            arr.push('<div class="op_ico time_num fl">' + data[i].YbjName + '</div>');

                            arr.push('</div>');
                            arr.push('</dd>');
                        }

                }
                oWrap.append(arr.join(''));
                if (total-columnConfig.show_num < columnConfig.page * columnConfig.show_num || columnConfig.page > columnConfig.load_num) {
                    window.removeEventListener('scroll', addcomment, false);
                }
                else {
                    oWrap.data('show', '0');
                }

                columnConfig.page++;
            } else {
                //columnConfig.iscolumn = false;
                if (total-columnConfig.show_num < columnConfig.page * columnConfig.show_num || columnConfig.page > columnConfig.load_num) {
                    window.removeEventListener('scroll', addcomment, false);
                }
                alert(rs.info);
            }
        }
        else {
        }
        pullLoaderConfig.oPullLoader.addClass('hide');
        pullLoaderConfig.loading = 0;

    }

    setTimeout(function () {
        navMore(navMoreConfig);
        scrollToTop(scrollToTopConfig);
        pullLoader();
    }, 300);
});